from collections import Counter
from sklearn.datasets import load_iris,load_digits,load_diabetes
import numpy as np
import matplotlib.pyplot as plt

def get_ent(dataset):
    """
    :param dataset: 数据集
    :return: 返回信息熵
    """
    # 标签
    targets = dataset[:,-1]
    target_count = Counter()
    # 不同标签的数量
    target_count.update(targets)
    length = len(dataset)
    ent = 0

    # ent += \sum^k(- pk*log_2(pk))
    for value in target_count.values():
        pk = value/length
        ent += - pk * np.math.log(pk,2)
    return ent

def choose_best_feature(dataset,features):
    """
    :param dataset: 数据集
    :param features: 数据的属性
    :return: 返回最优的属性
    """
    length = len(dataset)
    # 数据集的信息熵
    ent_D = get_ent(dataset)
    # 最优属性
    best_feature = 0
    # 所有属性中的最大信息增益
    max_gain = -1
    # 每个属性的最佳划分点
    best_huafen = []
    for feature in features:
        # 当前属性中的最大信息增益
        max_gain_part = -1
        featureList = np.sort(dataset[:,feature])
        # 取平均
        T_midu = set([(featureList[i]+featureList[i+1])/2 for i in range(len(featureList)-1) if featureList[i] != featureList[i+1]])
        best_huafen.append(0)
        for feat in T_midu:
            p = ent_D
            # 比划分点小的数据
            dataset_smaller = dataset[dataset[:,feature] < feat]
            ent_s = get_ent(dataset_smaller)
            # 比划分点大的数据
            dataset_bigger = dataset[dataset[:, feature] > feat]
            ent_b = get_ent(dataset_bigger)
            # 计算信息增益
            gain = p - (len(dataset_smaller)/length *ent_s + len(dataset_bigger)/length *ent_b)
            if(max_gain_part<gain):
                max_gain_part = gain
                best_huafen[feature] = feat
        if max_gain<max_gain_part:
            best_feature = feature
            max_gain = max_gain_part
    return best_feature,best_huafen[best_feature]

def createDecisonTree(dataset,features):
    targets = dataset[:,-1]
    # 如果所有标签都一样就不需要继续分叉
    if sum(targets==targets[0])==len(targets):
        return targets[0]
    # 如果该类数量太少就不继续进行分叉，直接选数据最多的标签作为叶节点，防止过拟合
    if len(dataset)<3:
        return Counter(targets).most_common(1)[0][0]

    # 获得最优属性
    best_feature, best_huafen = choose_best_feature(dataset, range(len(dataset[0])-1))

    decisionTree = {best_feature:{}}

    decisionTree[best_feature]["<{}".format(best_huafen)] = createDecisonTree(
        dataset[dataset[:, best_feature] < best_huafen], features)
    decisionTree[best_feature][">{}".format(best_huafen)] = createDecisonTree(
        dataset[dataset[:, best_feature] > best_huafen], features)

    return decisionTree

def test(data,tree,features):
    while True:
        for feat in features:
            if feat not in tree:
                continue
            tree = tree[feat]
            f = data[feat]
            keys = list(tree.keys())
            num = float(keys[0][1:])
            tree = tree[keys[0]] if f<num else tree[keys[1]]
            if type(tree) == np.float64:
                return tree

def predict(dataset,tree,features):
    targets = dataset[:,-1]
    ans = [test(data,tree,features) for data in dataset]
    right = 0
    length = len(ans)
    for i in range(length):
        if ans[i] == targets[i]:
            right += 1
    print('准确率',right / length)

def split_dataset(dataset,rate):
    """
    :param dataset: 数据集
    :param rate: 训练集的比率
    :return: 返回分割好的训练集和测试集
    """
    np.random.shuffle(dataset)
    return dataset[:int(len(dataset)*rate)],dataset[int(len(dataset)*rate):]

def getNumLeafs(myTree):
    numLeafs = 0
    firstStr = list(myTree.keys())[0]
    secondDict = myTree[firstStr]
    for key in secondDict.keys():
        if type(secondDict[
                    key]).__name__ == 'dict':  # test to see if the nodes are dictonaires, if not they are leaf nodes
            numLeafs += getNumLeafs(secondDict[key])
        else:
            numLeafs += 1
    return numLeafs


def getTreeDepth(myTree):
    maxDepth = 0
    firstStr = list(myTree.keys())[0]
    secondDict = myTree[firstStr]
    for key in secondDict.keys():
        if type(secondDict[
                    key]).__name__ == 'dict':  # test to see if the nodes are dictonaires, if not they are leaf nodes
            thisDepth = 1 + getTreeDepth(secondDict[key])
        else:
            thisDepth = 1
        if thisDepth > maxDepth: maxDepth = thisDepth
    return maxDepth


def plotNode(nodeTxt, centerPt, parentPt, nodeType):
    createPlot.ax1.annotate(nodeTxt, xy=parentPt, xycoords='axes fraction',
                            xytext=centerPt, textcoords='axes fraction',
                            va="center", ha="center", bbox=nodeType, arrowprops=arrow_args)


def plotMidText(cntrPt, parentPt, txtString):
    xMid = (parentPt[0] - cntrPt[0]) / 2.0 + cntrPt[0]
    yMid = (parentPt[1] - cntrPt[1]) / 2.0 + cntrPt[1]
    createPlot.ax1.text(xMid, yMid, txtString, va="center", ha="center", rotation=30)


def plotTree(myTree, parentPt, nodeTxt):  # if the first key tells you what feat was split on
    numLeafs = getNumLeafs(myTree)  # this determines the x width of this tree
    depth = getTreeDepth(myTree)
    firstStr = list(myTree.keys())[0]  # the text label for this node should be this
    cntrPt = (plotTree.xOff + (1.0 + float(numLeafs)) / 2.0 / plotTree.totalW, plotTree.yOff)
    plotMidText(cntrPt, parentPt, nodeTxt)
    plotNode(firstStr, cntrPt, parentPt, decisionNode)
    secondDict = myTree[firstStr]
    plotTree.yOff = plotTree.yOff - 1.0 / plotTree.totalD
    for key in secondDict.keys():
        if type(secondDict[
                    key]).__name__ == 'dict':  # test to see if the nodes are dictonaires, if not they are leaf nodes
            plotTree(secondDict[key], cntrPt, str(key))  # recursion
        else:  # it's a leaf node print the leaf node
            plotTree.xOff = plotTree.xOff + 1.0 / plotTree.totalW
            plotNode(secondDict[key], (plotTree.xOff, plotTree.yOff), cntrPt, leafNode)
            plotMidText((plotTree.xOff, plotTree.yOff), cntrPt, str(key))
    plotTree.yOff = plotTree.yOff + 1.0 / plotTree.totalD


# if you do get a dictonary you know it's a tree, and the first element will be another dict

def createPlot(inTree, name):
    fig = plt.figure(1, facecolor='white')
    fig.clf()
    axprops = dict(xticks=[], yticks=[])
    createPlot.ax1 = plt.subplot(111, frameon=False, **axprops)  # no ticks
    # createPlot.ax1 = plt.subplot(111, frameon=False) #ticks for demo puropses
    plotTree.totalW = float(getNumLeafs(inTree))
    plotTree.totalD = float(getTreeDepth(inTree))
    plotTree.xOff = -0.5 / plotTree.totalW;
    plotTree.yOff = 1.0;
    plotTree(inTree, (0.5, 1.0), '')
    # plt.savefig('13数据分布情况')
    plt.savefig(str(name))
    plt.show()

if __name__ == '__main__':
    decisionNode = dict(boxstyle="sawtooth", fc="0.8")
    leafNode = dict(boxstyle="round4", fc="0.8")
    arrow_args = dict(arrowstyle="<-")
    # 加载鸢尾花数据集
    iris = load_iris()
    data,targets = iris.data,iris.target
    # 将标签和数据拼接，方便处理数据
    dataset = np.hstack((data,targets.reshape((targets.shape[0],1))))

    # 对数据集进行分割
    train_dataset,test_dataset = split_dataset(dataset,0.7)

    # 建立决策树
    DecisionTree = createDecisonTree(train_dataset,range(len(data[0])))
    print(DecisionTree)
    # 测试
    predict(test_dataset,DecisionTree,range(len(data[0])))
    createPlot(DecisionTree, '鸢尾花数据集决策树')

    # 加载手写数字数据集
    digits = load_digits()
    data, targets = digits.data, digits.target
    # 将标签和数据拼接，方便处理数据
    dataset = np.hstack((data, targets.reshape((targets.shape[0], 1))))

    # 对数据集进行分割
    train_dataset, test_dataset = split_dataset(dataset, 0.7)

    # 建立决策树
    DecisionTree = createDecisonTree(train_dataset, range(len(data[0])))
    print(DecisionTree)
    # 测试
    predict(test_dataset, DecisionTree, range(len(data[0])))
    createPlot(DecisionTree, '手写数字数据集决策树')